<template>
  <div>
    <!-- 页码发生改变时触发   每页显示数量发生改变时触发 -->
    <el-pagination
      @current-change="currentChange"
      @size-change="sizeChange"
      :page-size="pageSize"
      :page-sizes="[2, 5, 10]"
      :total="total"
      layout="total,sizes, prev, pager, next,jumper"
    >
    </el-pagination>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="ID编号" prop="id" width="100"></el-table-column>
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column
        label="作者"
        prop="user.nickname"
        width="100"
      ></el-table-column>
      <el-table-column label="缩略图" width="200">
        <template slot-scope="scope">
          <img
            v-if="scope.row.cover.length > 0"
            class="thumbnail"
            :src="scope.row.cover[0].url | fixImgUrl"
            alt=""
          />
          <img v-else class="thumbnail" src="@/assets/logo.png" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope"
          ><el-button
            @click="$router.push('/editpost?id=' + scope.row.id)"
            size="small"
            type="warning"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageIndex: 1,
      pageSize: 5,
    };
  },
  created() {
    this.loadPage();
  },
  methods: {
    loadPage() {
      this.$axios({
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        console.log(res.data);
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
    },
    currentChange(newPage) {
      this.pageIndex = newPage;
      this.loadPage();
    },
    sizeChange(newPage) {
      this.pageSize = newPage;
      this.loadPage();
    },
  },
};
</script>

<style lang="less" scoped>
.thumbnail {
  width: 100%;
  height: 120px;
  object-fit: cover;
}
</style>